
sessionInfo()
# install.packages(c("data.table", "lubridate", "cowplot", "zoo", "Rbeast",
#                    "forecast", "abind", "reshape2"))

library(data.table)
library(lubridate)
library(tidyverse)
library(cowplot)
library(zoo)
library(Rbeast) ## uses version 0.9.3
library(forecast)

fb_data <- fread("fb_data.csv")


### Facebook Plots ###
tmp <- fb_data[,.(.N, sum(as.numeric(`Total Interactions`)), sum(Shares), 
                 sum(Likes), "Love" = sum(Love), "Wow"=sum(Wow), 
                 "Haha" = sum(Haha), "Sad"=sum(Sad), "Anger"=sum(Angry), "Care"=sum(Care), 
                 sum(Comments), "Angry_Posts"=sum(Angry > 0))
              , by=.(Party, lubridate::as_date(`Post Created`))]
tmp[lubridate==as.Date("2020-11-02")]
tmp[,mean(V3/N), by=Party]

monthly_fb <- (tmp[ , .(mean(N),
              "Reactions"=sum((V2-V3-V11)/sum(N)), 
              "Comments"=sum((V11)/sum(N)), 
              "Shares"=sum((V3)/sum(N)), 
              "Anger"=sum(Anger)/sum(V4 + Love + Wow+ Haha + Sad + Anger + Care), 
              "Angry_Posts"=sum(Angry_Posts)), 
              by=.(Party, cut(lubridate, breaks="month"))])

setorder(monthly_fb, cut, Party)

monthly_fb[cut %in% c("2021-07-01", "2020-07-01",
                      "2019-07-01", "2018-07-01",
                      "2017-07-01", "2016-07-01")]


col_pal <- c("Democratic"="skyblue3", 
             "Republican"="orangered3")
p1 <- ggplot(tmp, aes(x=lubridate, y=N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Number of Posts", x="")


p2 <- ggplot(tmp, aes(x=lubridate, y=(V2-V3-V11)/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), 
  #            linetype="dashed") + 
  labs(y="Reactions per Post", x="")

p3 <- ggplot(tmp, aes(x=lubridate, y=V11/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous() + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Comments per Post", x="")


p4 <- ggplot(tmp, aes(x=lubridate, y=V3/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) + 
  geom_smooth(method = loess, 
              method.args=list("span" =.5), 
              size=1) +
  theme_minimal() + 
  coord_cartesian(ylim=c(0, 100), expand = )+
  # scale_y_continuous(labels = scales::label_comma(accuracy = 1)) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Shares per Post", x="")


p5 <- get_legend(p1 + theme(legend.position = "bottom"))
plot <- plot_grid(plot_grid(p1 + theme(legend.position = "none") + ggtitle(""), 
                            p2 + theme(legend.position = "none") + ggtitle(""), 
                            p3 + theme(legend.position = "none") + ggtitle(""),
                            p4 + theme(legend.position = "none") + ggtitle(""), 
                            labels = c("Posts", "Reactions per Post", 
                                       "Comments per Post", "Shares per Post"), 
                            hjust=0, label_x = 0.01),
                  p5, 
                  rel_heights = c(1, .15), ncol = 1 )
plot
ggsave(plot = plot, bg = "white",
       filename = "all_posts.png",
       height=8, width=10)
### Looking at Anger #####

p1 <- ggplot(tmp, aes(x=lubridate, y=Anger/N, fill=Party, 
                color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous() + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Angry Reactions per Post", x="") 


p2 <- ggplot(tmp, aes(x=lubridate, y=Anger/(V2-V3-V11), fill=Party, 
                color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_percent()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Percentage of Angry Reactions", x="") 

p3 <- ggplot(tmp, aes(x=lubridate, y=Angry_Posts/N, fill=Party, 
                color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_percent()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Percentage of Angry Posts", x="") 


p4 <- ggplot(tmp, aes(x=lubridate, y=Angry_Posts, fill=Party, 
                color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous() + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Number of Angry Posts", x="") 


p5 <- get_legend(p1 + theme(legend.position = "bottom"))
plot <- plot_grid(plot_grid(p4 + theme(legend.position = "none") + ggtitle(""),
                            p3 + theme(legend.position = "none") + ggtitle(""),
                            p1 + theme(legend.position = "none") + ggtitle(""), 
                            p2 + theme(legend.position = "none") + ggtitle(""), 
                            labels = c("Total", "Percentage of Posts", "per Post", 
                                       "Percentage of Reactions"
                                       ), hjust=0, label_x = 0.01 ),
                  p5, 
                  rel_heights = c(1, .15), ncol = 1 )
ggsave(plot = plot, bg = "white",
       filename = "angry_facebook.png",
       height=8, width=10)


#### Estimation of BEAST ####

### Data prep
tmp <- fb_data[,.(.N, "shares"=mean(Shares), 
                 "comments"=mean(Comments), 
                 "int"=mean(`Total Interactions` - Shares - Comments))
              , by=.(Party, lubridate::as_date(`Post Created`))]

rep_tmp <- tmp[tmp$Party=="Republican",]
gop_ts <- zoo(rep_tmp[,-1:-2], rep_tmp$lubridate)
gop_ts$prop_shares <- (gop_ts$shares)
gop_ts$prop_comments <- (gop_ts$comments)
gop_ts$prop_int <- ((gop_ts$int))

dem_tmp <- tmp[tmp$Party=="Democratic",]
dem_ts <- zoo(dem_tmp[,-1:-2], dem_tmp$lubridate)
dem_ts$prop_shares <- (dem_ts$shares)
dem_ts$prop_comments <- (dem_ts$comments)
dem_ts$prop_int <- ((dem_ts$int))


x <- cbind(gop_ts$prop_comments, 
           gop_ts$prop_shares,
           gop_ts$prop_int,
           dem_ts$prop_comments, 
           dem_ts$prop_shares, 
           dem_ts$prop_int)

tsclean_drop <- function(x, lambda=NULL){
  outliers <- tsoutliers(x, lambda = lambda)
  x[outliers$index] <- NA
  return(x)
}


x <- apply(ts(x, freq=7), 2, function(x) tsclean_drop(x))
rownames(x) <- as.character(index(dem_ts))


### Estimation 
min_time <- 180

mod_rep_1 <- beast(x[,1], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
             mcmc.chains = 2, mcmc.seed = 711,
             torder.minmax=c(0, 1), tcp.minmax = c(0, 20))
mod_rep_2 <- beast(x[,2], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
                   mcmc.chains = 2, mcmc.seed = 711,
                   torder.minmax=c(0, 1), tcp.minmax = c(0, 20))
mod_rep_3 <- beast(x[,3], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
                   mcmc.chains = 2, mcmc.seed = 711,
                   torder.minmax=c(0, 1), tcp.minmax = c(0, 20))

mod_dem_1 <- beast(x[,4], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
                   mcmc.chains = 2, mcmc.seed = 711,
                   torder.minmax=c(0, 1), tcp.minmax = c(0, 20))
mod_dem_2 <- beast(x[,5], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
                   mcmc.chains = 2, mcmc.seed = 711,
                   torder.minmax=c(0, 1), tcp.minmax = c(0, 20))
mod_dem_3 <- beast(x[,6], season="harmonic", freq=7, tseg.min = min_time, sseg.min = min_time,
                   mcmc.chains = 2, mcmc.seed = 711,
                   torder.minmax=c(0, 1), tcp.minmax = c(0, 20))

# mod_dem <- beast123(x[,4:6], opt)



ts <- cbind(mod_rep_1$trend$Y, mod_rep_2$trend$Y, mod_rep_3$trend$Y,
            mod_dem_1$trend$Y, mod_dem_2$trend$Y, mod_dem_3$trend$Y)

tCI <- abind::abind(mod_rep_1$trend$CI, mod_rep_2$trend$CI, 
                    mod_rep_3$trend$CI, mod_dem_1$trend$CI,
                    mod_dem_2$trend$CI, mod_dem_3$trend$CI, 
                    along=3)

tcpCI <- abind::abind(mod_rep_1$trend$cpCI, mod_rep_2$trend$cpCI, 
                    mod_rep_3$trend$cpCI, mod_dem_1$trend$cpCI,
                    mod_dem_2$trend$cpCI, mod_dem_3$trend$cpCI, 
                    along=3)

tcpprob <-  abind::abind(mod_rep_1$trend$cpPr, mod_rep_2$trend$cpPr, 
                         mod_rep_3$trend$cpPr, mod_dem_1$trend$cpPr,
                         mod_dem_2$trend$cpPr, mod_dem_3$trend$cpPr, 
                         along=3)


# n_emp <- nrow(mod_dem$tcpCI[,,1]) - nrow(mod_rep$tcpCI[,,1])
# 
# rep_tcpCI <- array(NA, c(dim(mod_dem$tcpCI)[1],2,3))
# for(ii in 1:3){
#   rep_tcpCI[1:nrow(mod_rep$tcpCI[,,1]),,ii] <- mod_rep$tcpCI[,,ii]
# }
# 
# 
# tcpCI <- abind::abind(rep_tcpCI, mod_dem$tcpCI, along=3)
# 

colnames(ts) <- c("Comments_Republican", "Shares_Republican",
                  "Reactions_Republican",
                  "Comments_Democratic","Shares_Democratic", 
                  "Reactions_Democratic")
dimnames(tCI) <- list(as.character(rownames(x)), c("Low", "High"), 
                      
                      colnames(ts))

cis <- reshape2::melt(tCI) %>% separate(Var3, c("Metric", "Party"), sep="_") %>%
  pivot_wider(names_from="Var2", values_from=value) %>% rename("Day"=1)

med <- as_tibble(ts) %>% 
  add_column(time=rownames(x)) %>% 
  pivot_longer(cols=1:6,
               names_to=c(".value", "Party"),
               names_sep = "_") %>% 
  pivot_longer(3:5) %>% rename("Day"=1, "Mean"=4, "Metric"=3)

dimnames(tcpCI) <- list(1:dim(tcpCI)[1], c("Low", "High"),
                        colnames(ts))
cpts <-na.omit(reshape2::melt(tcpCI)) %>% as_tibble() %>% 
  separate(Var3, c("Metric", "Party"), sep="_") %>%
  pivot_wider(names_from="Var2", values_from=value) %>%
  mutate(Low=lubridate::as_date(Low, origin=rownames(x)[1]),
         High=lubridate::as_date(High, origin=rownames(x)[1]))



lim_data <- plyr::join(cis, med) %>% as_tibble() %>%
  mutate(Day=as.Date(Day)) %>% filter(Day > as.Date("2016-01-01"), 
                                      Day < as.Date("2021-01-01"))  %>% 
  mutate(Metric:=paste0(Metric))

tmp <- cpts %>% group_by(Metric, Party) %>% filter(High > as.Date("2016-01-01"), 
                                                   Low < as.Date("2021-01-01")) %>% 
  arrange(High)  %>%
  mutate(Metric:=paste0(Metric, "\nper Post")) %>% 
  mutate(inter = interval(Low, High)) 

### Finding overlaps 
input <- as.list(select(tmp, c(Metric, Party, inter)))
names(input) <- c("x", "y", "z")
tmp <- pmap_dfr(input, function(x, y, z, ...){
  tmp %>% filter(Metric!=x & Party == y) %>% 
    group_by(Metric) %>% 
    summarise("overlaps"=1*any(int_overlaps(z, inter))) %>% 
    pivot_wider(names_from=Metric,values_from=overlaps )  
} 
) %>% bind_cols(tmp) 

tmp <- tmp %>% 
  rowwise() %>% 
  mutate(overlap=sum(`Comments\nper Post`, `Reactions\nper Post`, `Shares\nper Post`, 
                     na.rm=T), 
         overlap=ifelse(overlap==2, "Overlapping Changepoint", "Changepoint")) %>% 
  select(-1:-3) 


plot <- tmp %>% mutate(Metric=gsub("\nper Post", "", Metric)) %>% 
  ggplot(aes(xmin=Low, xmax=High)) + 
  geom_rect(ymax=Inf, ymin=-Inf, aes(fill=overlap)) + 
  facet_grid(Metric~Party, scale="free_y") + 
  theme_minimal() + 
  theme(legend.position = "bottom") + 
  ylab("Daily Average per Post") +
  xlab("") + 
  scale_fill_brewer("", type = "qual", palette = 3) + 
  geom_ribbon(data = lim_data,
              aes(x=Day, y=Mean,  ymin=Low, ymax=High), 
              inherit.aes = F, fill="gray30", alpha=.8) 
plot

ggsave(plot = plot,
       bg = "white",
       filename = "changepoints.png", 
       height=5, width=7)

tmp %>% filter(overlap=="Overlapping Changepoint" & Party=="Republican" )


#### Account by Account Estimates ####

tmp_2 <- fb_data[, .(sum(.N),
                    "Reactions"= as.numeric(mean(`Total Interactions` - Shares - Comments)), 
                    "Comments"=as.numeric(mean(Comments)), 
                    "Shares"=as.numeric(mean(Shares))), 
                by=.(Party, 
                     `Page Name`,
                     cut(lubridate::as_date(`Post Created`), breaks="month"))]

tmp_out <- tibble(tmp_2) %>%  filter(V1 > 5) %>% 
  rename("Total Posts"="V1") %>% 
  pivot_wider(id_cols=c("Party", "Page Name"), 
              names_from=cut, 
              values_from=c("Total Posts", "Reactions", "Comments", "Shares")) %>%
  pivot_longer(cols = ends_with("01"), names_to=c("set", ".value"), 
               names_sep = "_") 

tmp_out <- tmp_out %>% 
  select(c("2016-07-01", "2019-07-01", "2018-07-01",
           "2020-07-01", "2017-07-01", "2021-07-01",
           "Party", "set", "Page Name")) %>%
  pivot_longer(cols = ends_with("01")) 


tab <- tmp_out %>% mutate(value=value) %>% 
  filter(set!="Total Posts") %>% group_by(Party, name, set) %>%
  summarise(value = list(value)) %>% 
  spread(Party, value) %>% 
  group_by(name, set) %>% 
  mutate(p_val = wilcox.test(unlist(Republican), unlist(Democratic), 
                             conf.int = T)$p.value,
         ci_low = wilcox.test(unlist(Republican), unlist(Democratic), 
                              conf.int = T)$conf.int[1], 
         ci_hi = wilcox.test(unlist(Republican), unlist(Democratic), 
                             conf.int = T)$conf.int[2], 
         N=length(na.omit(unlist(Republican))) + length(na.omit(unlist(Democratic))),
         Republican = median(unlist(Republican), na.rm=T),
         Democratic = median(unlist(Democratic), na.rm=T))

tab[tab$set=="Comments",]
tab[tab$set=="Reactions",]
tab[tab$set=="Shares",]


### Aggregate Statistics ###

fb_data[`Post Created` > as.Date("2019-01-01") &
         `Post Created` < as.Date("2019-07-01"), sum(Shares), by=Party]

fb_data[`Post Created` > as.Date("2018-01-01") &
         `Post Created` < as.Date("2018-07-01"), sum(Shares), by=Party]



#### Twitter #####


tw_data <- fread("twitter_data.csv")
tw_data[party=="Democrat", party:="Democratic"]

monthly_tw <- (tw_data[ , .(.N,
                            "Replies"=mean(reply_count), 
                            "Likes"=mean(like_count), 
                            "Qts"=mean(quote_count),
                            "RTs"=mean(retweet_count)), 
                    by=.(party, cut(`Post Created`, breaks="month"))])

setorder(monthly_tw, cut)

monthly_tw[cut %in% as.Date(c("2021-07-01", "2020-07-01",
                      "2019-07-01", "2018-07-01",
                      "2017-07-01", "2016-07-01"))]


tmp <- tw_data[, .(.N,
                  "Replies"=mean(reply_count), 
                  "Likes"=mean(like_count), 
                  "Qts"=mean(quote_count),
                  "RTs"=mean(retweet_count)), 
              by=.(party, `Post Created`)]

names(tmp)[1:2] <- c("Party", "lubridate")


col_pal <- c("Democratic"="skyblue3", 
             "Republican"="orangered3")
p1 <- ggplot(tmp, aes(x=lubridate, y=N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma(accuracy =1 )) +
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Number of Tweets", x="")


p2 <- ggplot(tmp, aes(x=lubridate, y=Replies, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma()) + 
  coord_cartesian(ylim=c(0, 5)) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), 
  #            linetype="dashed") + 
  labs(y="Replies per Tweet", x="")

p3 <- ggplot(tmp, aes(x=lubridate, y=Likes, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  coord_cartesian(ylim=c(0, 15)) + 
  # scale_y_continuous(trans="log10") + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Likes per Tweet", x="")


p4 <- ggplot(tmp, aes(x=lubridate, y=RTs, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) + 
  geom_smooth(size=1,method = loess,
              method.args=list("span" =.5)) + theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma(accuracy = 1)) +
  coord_cartesian(ylim=c(0, 10)) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Retweets per Tweet", x="")






p6 <- get_legend(p1 + theme(legend.position = "bottom"))

plot <- plot_grid(plot_grid(p1 + theme(legend.position = "none") + ggtitle(""), 
                            p2 + theme(legend.position = "none") + ggtitle(""), 
                            p3 + theme(legend.position = "none") + ggtitle(""),
                            p4 + theme(legend.position = "none") + ggtitle(""),
                            labels = c("Tweets", "Replies per Tweet", 
                                       "Likes per Tweet", "Retweets per Tweet"),
                            hjust=0, label_x = 0.01),
                  p6, 
                  rel_heights = c(1, .15), ncol = 1 )


plot

ggsave(plot = plot, bg = "white",
       filename = "all_twitter.png",
       height=8, width=10)


#### Facebook on Twitter ###




tmp <- fb_data[on_twitter==TRUE,.(.N, sum(as.numeric(`Total Interactions`)), sum(Shares), 
                    sum(Likes), sum(Love), sum(Wow), 
                    sum(Haha), sum(Sad), sum(Angry), sum(Care), 
                    sum(Comments))
                 , by=.(Party, lubridate::as_date(`Post Created`))]


col_pal <- c("Democratic"="skyblue3", 
             "Republican"="orangered3")
p1 <- ggplot(tmp, aes(x=lubridate, y=N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Number of Posts", x="")


p2 <- ggplot(tmp, aes(x=lubridate, y=(V2-V3-V11)/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma()) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), 
  #            linetype="dashed") + 
  labs(y="Reactions per Post", x="")

p3 <- ggplot(tmp, aes(x=lubridate, y=V11/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) +   geom_smooth(size=1,method = loess,
                                       method.args=list("span" =.5)) + 
  theme_minimal() + 
  scale_y_continuous() + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Comments per Post", x="")


p4 <- ggplot(tmp, aes(x=lubridate, y=V3/N, fill=Party, 
                      color=Party, shape=Party)) + 
  geom_point(alpha=.2) + 
  geom_smooth(size=1,method = loess,
              method.args=list("span" =.5)) + theme_minimal() + 
  scale_y_continuous(labels = scales::label_comma(accuracy = 1)) + 
  coord_cartesian(ylim=c(0, 100)) + 
  scale_color_manual("Party",
                     values=col_pal)  + 
  scale_fill_manual(values=col_pal) + 
  # geom_vline(xintercept = lubridate::as_date("2020-11-03"), linetype="dashed") + 
  labs(y="Shares per Post", x="")



p5 <- get_legend(p1 + theme(legend.position = "bottom"))
plot <- plot_grid(plot_grid(p1 + theme(legend.position = "none") + ggtitle(""), 
                            p2 + theme(legend.position = "none") + ggtitle(""), 
                            p3 + theme(legend.position = "none") + ggtitle(""),
                            p4 + theme(legend.position = "none") + ggtitle(""), 
                            labels = c("Posts", "Reactions per Post", 
                                       "Comments per Post", "Shares per Post"),
                            hjust=0, label_x = 0.01),
                  p5, 
                  rel_heights = c(1, .15), ncol = 1 )
plot
ggsave(plot = plot, bg = "white",
       filename = "fb_with_twitter_posts.png",
       height=8, width=10)

